﻿using System;
using System.Data;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class BillObject
    {
        public static Bill[] GetBills()
        {
            return Query(null);
        }

        public static Bill GetBill(int id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(BillDesc.Id, ConditionExpress.EQUALS, id);

            Bill[] set = Query(builder.ConditionString);

            if (set != null && set.Length > 0)
            {
                return set[0];
            }

            return null;
        }

        public static Bill[] Query(string condition)
        {
            // query factories from database
            string sql = "select * from " + BillDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where " + condition;
            }

            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            if (table != null && table.Rows.Count > 0)
            {
                Bill[] bills = new Bill[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    Bill bill = new Bill();

                    bill.Id                     = Convert.ToInt32(table.Rows[i][BillDesc.Id]);
                    bill.TableId                = Convert.ToInt32(table.Rows[i][BillDesc.TableId]);
                    bill.BusinessState          = Convert.ToInt32(table.Rows[i][BillDesc.BusinessState]);
                    bill.HeadCount              = Convert.ToInt32(table.Rows[i][BillDesc.HeadCount]);
                    bill.DiscountOptionId       = Convert.ToInt32(table.Rows[i][BillDesc.DiscountOptionId]);
                    bill.Pay                    = Convert.ToInt32(table.Rows[i][BillDesc.Pay]);
                    bill.TotalPrice             = Convert.ToInt32(table.Rows[i][BillDesc.TotalPrice]);
                    bill.CheckInTime            = Convert.ToDateTime(table.Rows[i][BillDesc.CheckInTime]);
                    bill.CheckOutTime           = Convert.ToDateTime(table.Rows[i][BillDesc.CheckOutTime]);
                    bill.ServiceTip             = Convert.ToInt32(table.Rows[i][BillDesc.ServiceTip]);
                    bill.CheckOutMode           = Convert.ToInt32(table.Rows[i][BillDesc.CheckOutMode]);

                    bills.SetValue(bill, i);
                }

                return bills;
            }

            return null;
        }

        public static Int32 Add(Bill bill)
        {
            SqlBuilder builder = new SqlBuilder();

            builder.InsertAdd(BillDesc.TableId,             bill.TableId);
            builder.InsertAdd(BillDesc.BusinessState,       bill.BusinessState);
            builder.InsertAdd(BillDesc.HeadCount,           bill.HeadCount);
            builder.InsertAdd(BillDesc.DiscountOptionId,    bill.DiscountOptionId);
            builder.InsertAdd(BillDesc.Pay,                 bill.Pay);
            builder.InsertAdd(BillDesc.TotalPrice,          bill.TotalPrice);
            builder.InsertAdd(BillDesc.CheckInTime,         bill.CheckInTime);
            builder.InsertAdd(BillDesc.CheckOutTime,        bill.CheckOutTime);
            builder.InsertAdd(BillDesc.ServiceTip,          bill.ServiceTip);
            builder.InsertAdd(BillDesc.CheckOutMode,        bill.CheckOutMode);

            builder.QueryTableName = BillDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

        public static bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(BillDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = BillDesc.TableName;
            string sql = builder.DeleteCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public static Int32 Update(Bill bill)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.UpdateAdd(BillDesc.TableId,                  bill.TableId);
            builder.UpdateAdd(BillDesc.BusinessState,            bill.BusinessState);
            builder.UpdateAdd(BillDesc.HeadCount,                bill.HeadCount);
            builder.UpdateAdd(BillDesc.DiscountOptionId,         bill.DiscountOptionId);
            builder.UpdateAdd(BillDesc.Pay,                      bill.Pay);
            builder.UpdateAdd(BillDesc.TotalPrice,               bill.TotalPrice);
            builder.UpdateAdd(BillDesc.CheckInTime,              bill.CheckInTime);
            builder.UpdateAdd(BillDesc.CheckOutTime,             bill.CheckOutTime);
            builder.UpdateAdd(BillDesc.ServiceTip,               bill.ServiceTip);
            builder.UpdateAdd(BillDesc.CheckOutMode,             bill.CheckOutMode);
            builder.ConditionAdd(BillDesc.Id, ConditionExpress.EQUALS, bill.Id);
            builder.QueryTableName = BillDesc.TableName;

            string sql = builder.UpdateCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }
    }
}
